﻿using EMall.Domain;
using Microsoft.AspNetCore.Identity;
using System.Text.Json;

namespace EMall.IdentityService.Roles
{
    public class Role : IdentityRole<int>, IAggregateRoot<int>
    {
        public virtual bool Enabled { get; private set; }
        public Role(string name, bool enabled) : base(name)
        {
            Enabled = enabled;
            AddDomainEvent(new EntityCreatedDomainEvent<Role>(this));
        }
        public void Update(string name, bool enabled)
        {
            var originJson = JsonSerializer.Serialize(this);
            Name = name;
            Enabled = enabled;
            AddDomainEvent(new EntityUpdatedDomainEvent<Role>(originJson, this));
        }
        public IReadOnlyList<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();
        protected List<IDomainEvent> _domainEvents = new List<IDomainEvent>();
        protected virtual void AddDomainEvent(IDomainEvent eventItem) => _domainEvents.Add(eventItem);
        public virtual void ClearDomainEvents() => _domainEvents.Clear();
    }
}
